package com.ruoyi.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.li.work.mapper.SysUserMapper;
import com.ruoyi.teaching.domain.PlatformLLearn;
import com.ruoyi.teaching.domain.PlatformUVirtualLearnTime;
import com.ruoyi.teaching.domain.UniversalCreateUpdate;
import com.ruoyi.teaching.mapper.PlatformUVirtualLearnTimeMapper;
import com.ruoyi.teaching.service.PlatformUVirtualLearnTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.teaching.utils.UniversalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王旭
 * @since 2025-02-19
 */
@Service
public class PlatformUVirtualLearnTimeServiceImpl extends ServiceImpl<PlatformUVirtualLearnTimeMapper, PlatformUVirtualLearnTime> implements PlatformUVirtualLearnTimeService {
    @Autowired
    private PlatformUVirtualLearnTimeMapper platformUVirtualLearnTimeMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisCache redisCache;
    private static final String VIRTUAL_SIMULATION_KEY = "learning:virtual";

    @Override
    public AjaxResult listPage(Integer pageNum, Integer pageSize, Long productId) {
        Page<PlatformUVirtualLearnTime> platformUVirtualLearnTimePage = new Page<>(pageNum, pageSize);
        Long userId = SecurityUtils.getUserId();
        QueryWrapper<PlatformUVirtualLearnTime> virtualLearnTimeQueryWrapper = new QueryWrapper<>();
        virtualLearnTimeQueryWrapper.eq("pid", productId);
        virtualLearnTimeQueryWrapper.eq("uid", userId);
        virtualLearnTimeQueryWrapper.orderByDesc("end_time");
        Page<PlatformUVirtualLearnTime> learnTimePage = platformUVirtualLearnTimeMapper.selectPage(platformUVirtualLearnTimePage, virtualLearnTimeQueryWrapper);
        return new AjaxResult(HttpStatus.SUCCESS, "查询成功", learnTimePage);

    }

    @Override
    public AjaxResult addLearnTime(PlatformUVirtualLearnTime platformUVirtualLearnTime) {
        UniversalCreateUpdate create = UniversalUtil.getCreateUpdate(sysUserMapper);
        platformUVirtualLearnTime.setCreateTime(DateUtils.getNowDate());
        platformUVirtualLearnTime.setCreateId(Long.valueOf(create.getId()));
        platformUVirtualLearnTime.setCreateBy(create.getName());
        int insert = platformUVirtualLearnTimeMapper.insert(platformUVirtualLearnTime);
        if (insert > 0) {
            redisCache.removeCacheSetValue(VIRTUAL_SIMULATION_KEY, create.getId());
            return new AjaxResult(HttpStatus.SUCCESS, "添加成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "添加失败");
        }
    }

    @Override
    public AjaxResult deleteLearnTime(Long id) {
        int i1 = platformUVirtualLearnTimeMapper.deleteById(id);
        if (i1 > 0) {
            return new AjaxResult(HttpStatus.SUCCESS, "删除成功");
        } else {
            return new AjaxResult(HttpStatus.ERROR, "删除失败");
        }
    }
}
